import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQuick.Layouts

Rectangle {
    Layout.fillWidth: true
    Layout.fillHeight: true

    ColumnLayout{
        anchors.fill: parent
        spacing: 10

        RowLayout {
            Layout.fillWidth: true
            Layout.minimumHeight: 30
            Layout.margins: 10
            Button {
                text: "追加文本"
                onClicked: {
                    textArea.append("【追加文本】这是一个测试文本，用于演示滚动功能。\n".repeat(5))
                }
            }
        }

        ScrollView {
            id: scrollView
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.margins: 10

            ScrollBar.vertical: ScrollBar {
                anchors.top: scrollView.top
                anchors.bottom: scrollView.bottom
                anchors.right: scrollView.right
                policy: ScrollBar.AlwaysOn

                // 保持滚动到底端
                position: {
                    if (scrollView.height > 0 && scrollView.contentHeight > scrollView.height) {
                        return (scrollView.contentHeight - scrollView.height) / scrollView.contentHeight;
                    }
                    return 0;
                }
            }

            TextArea {
                id: textArea
                wrapMode: TextArea.Wrap
                text: "这是一个测试文本，用于演示滚动功能。\n".repeat(30)
                selectByMouse: true
                mouseSelectionMode: TextEdit.SelectCharacters

                background: Rectangle {
                    color: textArea.enabled ? "white" : "#f5f5f5"
                    border.color: textArea.activeFocus ? "#1296FF" : "#c0c0c0"
                    border.width: textArea.activeFocus ? 2 : 1
                    radius: 4
                }
            }
        }
    }
}
